package com.xiyan.blog.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiyan.blog.entity.Comment;
import com.xiyan.blog.service.ICommentService;
import com.xiyan.blog.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@Api(value = "评论", tags = "评论api")
public class CommentController {
    private static final Logger log = LoggerFactory.getLogger(CommentController.class);

    @Resource
    private ICommentService commentService;

    @ApiOperation("新增评论")
    @RequestMapping(value = "/add/{parentId}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Comment comment, @PathVariable("parentId") String parentId) {
        try {
            if (ObjectUtil.isNull(comment)) {
                return ResponseResult.error().message("新增的评论为空");
            }

            // 该评论为顶层评论，只有子评论，无父评论
            if (StrUtil.isNotEmpty(parentId)) {
                // 该评论为parentId下的子评论
                comment.setParentId(parentId);
            }

            commentService.saveOrUpdate(comment);
            return ResponseResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("新增失败");
        }
    }

    @ApiOperation("删除评论")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseResult delete(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().message("评论id为空");
            }
            commentService.removeById(id);
            return ResponseResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("删除失败，评论ID不存在");
        }
    }

    @ApiOperation("获取评论列表")
    @RequestMapping(value = "/{blogId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list(@PathVariable("blogId") String blogId) {
        try {
            if (StrUtil.isEmpty(blogId)) {
                return ResponseResult.error().message("博客id为空");
            }

            List<Comment> comments = commentService.findByBlogId(blogId);
            Map<String, Comment> map = new HashMap<>();
            List<Comment> result = new ArrayList<>();

            for (Comment comment : comments) {
                if (StrUtil.isEmpty(comment.getParentId())) {
                    result.add(comment);
                }
                map.put(comment.getId(), comment);
            }

            for (Comment comment : comments) {
                if (StrUtil.isNotEmpty(comment.getParentId())) {
                    Comment parent = map.get(comment.getParentId());
                    if (CollUtil.isEmpty(parent.getChildComment())) {
                        parent.setChildComment(new ArrayList<Comment>());
                    }
                    parent.getChildComment().add(comment);
                }
            }

            return ResponseResult.ok().data(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取评论列表失败");
        }
    }

    @ApiOperation("获取评论信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult getById(@PathVariable("id") String id) {
        try {
            if (id == null) {
                return ResponseResult.error().message("评论id为空");
            }

            Comment comment = commentService.getById(id);
            return ResponseResult.ok().data(comment);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取评论信息失败");
        }
    }

    // TODO: 多条件分页查询
}
